import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
let store = new Vuex.Store({
  state: {
    username: '',
    token: '',
    userId: '',
    productList: [], //存放分类数据列表的商品,
    categoryName: '小米手机',
    shopcartCount: 0,
  },
  getters:{
    getProductList(state){
      return state.productList;
    }
  },

  mutations:{
    //退出登录时， 控制username， token的状态
    login(state, payload){
      //设置在浏览器的内存中  localStroage关闭浏览器数据还存在， sessionStorage
      localStorage.setItem('Authentication', payload.token);
      localStorage.setItem('username', payload.username);
      localStorage.setItem('userId', payload.userId);
      // sessionStorage.setItem('username', JSON.stringify(payload.username));

      //设置vuex里面的state的username和token
      state.username = localStorage.getItem('username');
      state.token = localStorage.getItem('Authentication');
      state.userId = localStorage.getItem('userId');
    },
    //处理退出登录时， 删除username， token的信息
    logout(state){
      //删除浏览器的用户信息
      localStorage.removeItem('Authentication');
      localStorage.removeItem('username');
      localStorage.removeItem('userId');
      //删除状态中用户的信息
      state.username = "";
      state.token = "";
      state.userId = '';
      state.shopcartCount = 0;
    },

    changeProductList(state, data){
      // console.log(data);
      state.productList = data;
    },

    changeCategoryName(state, name){
      state.categoryName = name;
    },

    setShopcartCount(state, count){
      state.shopcartCount += count;
    }
  },

  actions:{

  }
});
export default store
